//
//  ManageSetViewController.m
//  leo
//
//  Created by RyanLin on 11/8/15.
//  Copyright © 2015 mdx. All rights reserved.
//

#import "ManageSetViewController.h"
#import "Setting.h"
#import "SyncManager.h"
#import "LeoMsg.h"
@interface ManageSetViewController ()
@property(nonatomic,strong) SyncManager *syncManager;
@property(nonatomic,strong) Watch *selectWatch;

@end

@implementation ManageSetViewController

@synthesize navLeftButton;
@synthesize syncManager;
@synthesize selectWatch;
-(void)dealloc
{
    syncManager=NULL;
    selectWatch=NULL;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.title=@"设置";
    [self initNavButton];
    User *user=[User sharedSingleton];
    if(user==NULL)
        return;
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager!=NULL)
        selectWatch=[dataManager getActiveWatch:user.uid];
    syncManager=[[SyncManager alloc] init];
    [syncManager getSettingParameters:selectWatch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if(result==SUCESS)
        {
            if(dataManager!=NULL)
                selectWatch=[dataManager getActiveWatch:user.uid];
            [self initUI];
        }
        else
        {
            [Tooler showHttpMsg:result description:description msg:@"手表参数获取失败"];
        }
    }];
    [self initUI];
    
    // Do any additional setup after loading the view from its nib.
}
- (void)initNavButton {
    [navLeftButton setTarget:self];
    [navLeftButton setAction:@selector(back)];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
    //[self dismissViewControllerAnimated:YES completion:nil];
}

-(void)initUI
{
    if(selectWatch==NULL)
        return;
    if(selectWatch.messagePush==0)
        self.msgSwitch.on=NO;
    else if(selectWatch.messagePush==1)
        self.msgSwitch.on=YES;
    
    if(selectWatch.taskPush==0)
        self.taskSwitch.on=NO;
    else
        self.taskSwitch.on=YES;
    
    if (selectWatch.shortNum == 0) {
        self.shortNumSwitch.on = NO;
    }
   else
   {
       self.shortNumSwitch.on = YES;
   }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)setTask:(id)sender {
    if(syncManager==NULL)
        return;
    if(selectWatch==NULL)
        return;
    Setting *taskSet=[[Setting alloc] init];
    if(taskSet==NULL)
        return;
    self.taskSwitch.enabled=NO;
    BOOL taskPush=[self.taskSwitch isOn];
    taskSet.value=taskPush;
    taskSet.type=2;
    [syncManager updateSettingParameters:selectWatch.deviceUid Setting:taskSet completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
       if(result==SUCESS)
       {
           if(taskPush)
               [LeoMsg showMsg:@"任务推送被开启"];
           else
               [LeoMsg showMsg:@"任务推送被关闭"];
       }
       else
        {
            [Tooler showHttpMsg:result description:description msg:@"任务推送设置失败"];
        }
        self.taskSwitch.enabled=YES;
    }];
}

- (IBAction)setMessage:(id)sender {
    if(syncManager==NULL)
        return;
    if(selectWatch==NULL)
        return;
    Setting *msgSet=[[Setting alloc] init];
    if(msgSet==NULL)
        return;
    self.msgSwitch.enabled=NO;
    msgSet.type=0;
    msgSet.value=[self.msgSwitch isOn];
    [syncManager updateSettingParameters:selectWatch.deviceUid Setting:msgSet completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if(result==SUCESS)
        {
            if(msgSet.value==1)
                [LeoMsg showMsg:@"智能推送被开启"];
            else
                [LeoMsg showMsg:@"智能推送被关闭"];
        }
        else
        {
            [Tooler showHttpMsg:result description:description msg:@"智能推送设置失败"];
        }
        self.msgSwitch.enabled=YES;
    }];
}

//UI更改
- (IBAction)shortNumSwitchAction:(id)sender {
    if(syncManager==NULL)
        return;
    if(selectWatch==NULL)
        return;
    Setting *shortNumSet=[[Setting alloc] init];
    if(shortNumSet==NULL)
        return;
    self.msgSwitch.enabled=NO;
    shortNumSet.type=4;
    shortNumSet.value=[self.shortNumSwitch isOn];
    [syncManager updateSettingParameters:selectWatch.deviceUid Setting:shortNumSet completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if(result==SUCESS)
        {
            if(shortNumSet.value==1)
                [LeoMsg showMsg:@"屏蔽短号码功能被开启"];
            else
                [LeoMsg showMsg:@"屏蔽短号码功能被关闭"];
        }
        else
        {
            [Tooler showHttpMsg:result description:description msg:@"智能推送设置失败"];
        }
        self.msgSwitch.enabled=YES;
    }];

}

- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
-(void)viewWillAppear:(BOOL)animated{
    
//    self.hidesBottomBarWhenPushed = NO;
//    
//    UIColor *blueBackgroundColor=[UIColor colorWithRed:71/255.0 green:178/255.0 blue:247/255.0 alpha:1];
//    UIImage *blueBackgroundImage=[self imageWithColor:blueBackgroundColor];
//    
//    UINavigationBar *navigationBar = self.navigationController.navigationBar;
//    // white.png图片自己下载个纯白色的色块，或者自己ps做一个
//    [navigationBar setBackgroundImage:blueBackgroundImage
//                       forBarPosition:UIBarPositionAny
//                           barMetrics:UIBarMetricsDefault];
//    [navigationBar setShadowImage:[UIImage new]];
    
    
    float width=[UIScreen mainScreen].bounds.size.width;
    UIImageView *shadow=[[UIImageView alloc]initWithFrame:CGRectMake(0, 65, width, 1)];
    UIColor *blueBackgroundColor=[UIColor colorWithRed:70/255.0 green:159/255.0 blue:248/255.0 alpha:1];
    shadow.image=[self imageWithColor:blueBackgroundColor];
    
    //[self.view addSubview:shadow];
}

@end
